package com.caocaog.market.dms.common;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caocaog.market.dms.dto.OrderSendDto;
import com.caocaog.market.dms.dto.OrderValuationDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DmsUtils {

    private static final String LOCAL_URL="192.168.1.33:8088/ilaile";//本地链接
    private static final String SERVER_URL="https://dms.caocaog.com/ilaile";//线上链接
    private static final String REQUEST="/open/caocaolaile";//请求
    private static final String LOGIN="/bindUser";//绑定跑腿账户
    private static final String BALANCE="/queryBalance";//查询余额
    private static final String VALUATION="/orderValuation";//订单计价
    private static final String SEND="/orderSend";//订单发单
    private static final String CANCEL="/orderCancel";//取消订单
    private static final String QUERY="/orderQuery";//取消订单


    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final String REQUEST_BODY = "requestBody";
    private static final String REQUEST_HEAD = "requestHead";

    @Value("${spring.profiles.active}")
    private void setEnv(String env) {
        init(env);
    }

    private static String bindUserUrl;
    private static String queryBalanceUrl;
    private static String orderValuationUrl;
    private static String orderSendUrl;
    private static String orderCancelUrl;
    private static String orderQueryUrl;

    public static void init(String env) {
        log.info("DmsUtils.init");
        if ("prod".equals(env)) {
            bindUserUrl=SERVER_URL+REQUEST+LOGIN;
            queryBalanceUrl=SERVER_URL+REQUEST+BALANCE;
            orderValuationUrl=SERVER_URL+REQUEST+VALUATION;
            orderSendUrl=SERVER_URL+REQUEST+SEND;
            orderCancelUrl=SERVER_URL+REQUEST+CANCEL;
            orderQueryUrl=SERVER_URL+REQUEST+QUERY;
        } else {
            bindUserUrl=LOCAL_URL+REQUEST+LOGIN;
            queryBalanceUrl=LOCAL_URL+REQUEST+BALANCE;
            orderValuationUrl=LOCAL_URL+REQUEST+VALUATION;
            orderSendUrl=LOCAL_URL+REQUEST+SEND;
            orderCancelUrl=LOCAL_URL+REQUEST+CANCEL;
            orderQueryUrl=LOCAL_URL+REQUEST+QUERY;
        }
    }

    public static String bindUer(String username,String password){
        JSONObject object = new JSONObject();
            object.put("username", username);
            object.put("password", password);

        HttpResponse execute = HttpRequest.post(bindUserUrl)
                .body(object.toJSONString()).execute();
        return execute.body();
    }

    public static String queryBalance(String uid,String key){
        JSONObject object = new JSONObject();
        object.put("uid", uid);

        HttpResponse execute = HttpRequest.post(queryBalanceUrl)
                .body(object.toJSONString())
                .header("X-Access-Token", key)
                .execute();
        return execute.body();
    }

    public static String orderValuation(OrderValuationDto orderValuationDTO, String key){

        JSONObject object = new JSONObject();
        object.put("shopId", orderValuationDTO.getShopId());
        object.put("isDelay", orderValuationDTO.getIsDelay());
        object.put("delayTime", orderValuationDTO.getDelayTime());
        object.put("goodType", orderValuationDTO.getGoodType());
        object.put("goodsWeight", orderValuationDTO.getGoodsWeight());
        object.put("fromName", orderValuationDTO.getFromName());
        object.put("fromMobile", orderValuationDTO.getFromMobile());
        object.put("fromAddress", orderValuationDTO.getFromAddress());
        object.put("fromLng", orderValuationDTO.getFromLng());
        object.put("fromLat", orderValuationDTO.getFromLat());
        object.put("toName", orderValuationDTO.getToName());
        object.put("toMobile", orderValuationDTO.getToMobile());
        object.put("toAddress", orderValuationDTO.getToAddress());
        object.put("toLng", orderValuationDTO.getToLng());
        object.put("toLat", orderValuationDTO.getToLat());
        object.put("canhao", orderValuationDTO.getCanhao());


        HttpResponse execute = HttpRequest.post(orderValuationUrl)
                .body(object.toJSONString())
                .header("X-Access-Token", key)
                .execute();
        return execute.body();
    }

    public static String orderSend(OrderSendDto sendOrder, String key){

        JSONObject object = new JSONObject();
        object.put("shopId", sendOrder.getShopId());
        object.put("orderNo", sendOrder.getOrderNo());
        object.put("orderSource", sendOrder.getOrderSource());
        object.put("originOrderNo", sendOrder.getOriginOrderNo());
        object.put("isDelay", sendOrder.getIsDelay());
        object.put("delayTime", sendOrder.getDelayTime());
        object.put("goodType", sendOrder.getGoodType());
        object.put("goodsWeight", sendOrder.getGoodsWeight());
        object.put("fromName", sendOrder.getFromName());
        object.put("fromMobile", sendOrder.getFromMobile());
        object.put("fromAddress", sendOrder.getFromAddress());
        object.put("fromLng", sendOrder.getFromLng());
        object.put("fromLat", sendOrder.getFromLat());
        object.put("toName", sendOrder.getToName());
        object.put("toMobile", sendOrder.getToMobile());
        object.put("toAddress", sendOrder.getToAddress());
        object.put("toLng", sendOrder.getToLng());
        object.put("toLat", sendOrder.getToLat());
        object.put("remark", sendOrder.getRemark());
        object.put("canhao", sendOrder.getCanhao());


        HttpResponse execute = HttpRequest.post(orderSendUrl)
                .body(object.toJSONString())
                .header("X-Access-Token", key)
                .execute();
        return execute.body();
    }

    public static String orderCancel(String orderNo, String key){

        JSONObject object = new JSONObject();
        object.put("orderNo", orderNo);
        HttpResponse execute = HttpRequest.post(orderCancelUrl)
                .body(object.toJSONString())
                .header("X-Access-Token", key)
                .execute();
        return execute.body();
    }

    public static String orderQuery(String orderNo, String key){

        JSONObject object = new JSONObject();
        object.put("orderNo", orderNo);
        HttpResponse execute = HttpRequest.post(orderQueryUrl)
                .body(object.toJSONString())
                .header("X-Access-Token", key)
                .execute();
        return execute.body();
    }


 // public static void valuation(){
   //   String key = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Nzk1Njc3ODIsInVzZXJuYW1lIjoiMTUzMjI5NjY1MTU4ODkzMTU4NSJ9.nzMhYOjlb34dURe5m0mknPf5Ydsll3l7_ZBFz1NSMW0";
//      String successData=  bindUer("18624958559","q294528130");
//      JSONObject successJSON = JSON.parseObject(successData);
//      Integer code = successJSON.getInteger("code");
//      if(code==200){
//          JSONObject  resultObj=  successJSON.getJSONObject("result");
//          String uid = resultObj.getString("uid");
//          String key = resultObj.getString("key");
//          if(Objects.nonNull(uid)&& Objects.nonNull(key)){
//              System.out.println(uid+key);
//          }
//      }
//
//      String s=  queryBalance("1532296651588931585", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Nzk1Njc3ODIsInVzZXJuYW1lIjoiMTUzMjI5NjY1MTU4ODkzMTU4NSJ9.nzMhYOjlb34dURe5m0mknPf5Ydsll3l7_ZBFz1NSMW0");
//      System.out.println(s);
//      OrderValuationDto preOrderDTO = new OrderValuationDto();
//      preOrderDTO.setCanhao("");
//      preOrderDTO.setIsDelay(0);
//      preOrderDTO.setDelayTime("");
//      preOrderDTO.setFromAddress("正商经开广场2号楼");
//      preOrderDTO.setFromLat("34.719585418701186");
//      preOrderDTO.setFromLng("113.77022552490234");
//      preOrderDTO.setFromMobile("15893681899");
//      preOrderDTO.setFromName("李晨阳");
//      preOrderDTO.setGoodType("xiaochi");
//      preOrderDTO.setGoodsWeight(new BigDecimal(0.5));
//      preOrderDTO.setToName("救济金");
//      preOrderDTO.setToAddress("正商经开广场");
//      preOrderDTO.setToMobile("15893681899");
//      preOrderDTO.setToAddress("正商和谐大厦");
//      preOrderDTO.setToLat("34.741886");
//      preOrderDTO.setToLng("113.777348");
//
//
//      String ovdata = orderValuation(preOrderDTO, key);
//      JSONObject successJSON = JSON.parseObject(ovdata);
//      Integer code = successJSON.getInteger("code");
//      if (code == 200) {
//          JSONObject resultObj = successJSON.getJSONObject("result");
//          BigDecimal amount = resultObj.getBigDecimal("amount")
//                  .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
//          BigDecimal coupon = resultObj.getBigDecimal("coupon")
//                  .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
//          BigDecimal distance = resultObj.getBigDecimal("distance")
//                  .divide(BigDecimal.valueOf(1000),2,BigDecimal.ROUND_DOWN);
//          OrderValuationVo vo = new OrderValuationVo();
//          vo.setAmount(amount);
//          vo.setCoupon(coupon);
//          vo.setDistance(distance);
//          System.out.println("amount:"+vo.getAmount()+"coupon:"+vo.getCoupon()+"dis:"+vo.getDistance());
//      }else {
//          System.out.println(ovdata);
//      }
//  }
//
//  public static void  send(){
//        String key = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Nzk1Njc3ODIsInVzZXJuYW1lIjoiMTUzMjI5NjY1MTU4ODkzMTU4NSJ9.nzMhYOjlb34dURe5m0mknPf5Ydsll3l7_ZBFz1NSMW0";
//        OrderSendDto sendDto= new OrderSendDto();
//
//        sendDto.setShopId("10001");
//        sendDto.setOrderNo("230002131212121212121");
//        sendDto.setOriginOrderNo("230002131212121212121");
//        sendDto.setCanhao("");
//        sendDto.setOrderSource("125");
//        sendDto.setIsDelay(0);
//        sendDto.setDelayTime("");
//        sendDto.setFromAddress("正商经开广场2号楼");
//        sendDto.setFromLat("34.719585418701186");
//        sendDto.setFromLng("113.77022552490234");
//        sendDto.setFromMobile("15893681899");
//        sendDto.setFromName("李晨阳");
//        sendDto.setGoodType("xiaochi");
//        sendDto.setGoodsWeight(new BigDecimal(0.5));
//        sendDto.setToName("救济金");
//        sendDto.setToAddress("正商经开广场");
//        sendDto.setToMobile("15893681899");
//        sendDto.setToAddress("正商和谐大厦");
//        sendDto.setToLat("34.741886");
//        sendDto.setToLng("113.777348");
//        sendDto.setRemark("小心取货");
//
//        String ovdata = orderSend(sendDto, key);
//        JSONObject successJSON = JSON.parseObject(ovdata);
//        Integer code = successJSON.getInteger("code");
//        if (code == 200) {
//            JSONObject resultObj = successJSON.getJSONObject("result");
//            BigDecimal amount = resultObj.getBigDecimal("amount")
//                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
//            BigDecimal coupon = resultObj.getBigDecimal("coupon")
//                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
//            BigDecimal payAmount = resultObj.getBigDecimal("payAmount")
//                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
//           String ptOrder= resultObj.getString("orderId");
//            String orderNo= resultObj.getString("caocaolaileOrderNo");
//        OrderSendVo orderSendVo =new OrderSendVo();
//        orderSendVo.setAmount(String.valueOf(amount));
//        orderSendVo.setCoupon(String.valueOf(coupon));
//        orderSendVo.setPayAmount(String.valueOf(payAmount));
//        orderSendVo.setPtOrderNo(ptOrder);
//        orderSendVo.setOrderNo(orderNo);
//            System.out.println(orderSendVo);
//        }else {
//            System.out.println(ovdata);
//        }
//    }


    public static void main(String[] args) {
        init("prod");

        String key = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Nzk1Njc3ODIsInVzZXJuYW1lIjoiMTUzMjI5NjY1MTU4ODkzMTU4NSJ9.nzMhYOjlb34dURe5m0mknPf5Ydsll3l7_ZBFz1NSMW0";

        String ovdata =    orderQuery("23040416154031627101525959", key);
        JSONObject successJSON = JSON.parseObject(ovdata);
        Integer code = successJSON.getInteger("code");
        if (code == 200) {
            System.out.println(successJSON);
        }

    }
}
